// 引入 react 依赖
import React, { Component } from 'react';
import { Provider, connect } from 'react-redux';

import { StyleSheet, TextInput, Text, ScrollView, View, Platform, StatusBar, InteractionManager, NativeModules } from 'react-native';
const { StatusBarManager } = NativeModules;

import { Scene, Router, Actions, Reducer, ActionConst, Overlay, Tabs, Modal, Drawer, Stack, Lightbox } from 'react-native-router-flux';

// 引入 redux 所需文件
import store from './src/store/store.js'
// import SimpleApp from './src/router/router.js'

// import { AppId } from './src/util/utils.js'

// 引入第三方依赖
// import _ from 'lodash';
import * as WeChat from 'react-native-wechat';
import pureRender from 'pure-render-decorator';
import SplashScreen from 'react-native-splash-screen'
import * as ActionTypes from './src/constants/constants.js';
import * as AntDesign from '@ant-design/react-native';


// 引入组件
import {
  Advertise,
  Home,
  Guest,
  Health,
  Mall,
  Data,
  User,
  GoodDetails,
  Login,
  PhoneLogin,
  Invite,
  ShoppingCart,
  SubmitOrder,
  PaymentResult,
  Categories,
  Order,
  Search,
  SearchResults,
  Address,
  CreatAddress,
  EditAddress,
  Appraise,
  OrderInfo,
  DemoLightbox,
  CvList,
  CoinList,
  Integral,
  IntegralRanking,
  CoinRanking,
  AgentGoods,
  AgentProduct,
  UserInfo,
  Setting,
  ChangePassword,
  SelectPay,
  MyRelatives,
  AddMyRelatives,
  InviteFriends,
  Invoice,
  EditInvoice,
  CreatInvoice,
  Registered,
  SetUserInfo,
  PlateDetail,
  CardPostDetail,
  PostCard,
  CardMyList,
  CardUserInfo,
  CollectionCard,
  FansList,
  Feedback,
  FollowAccount,
  FollowPlate,
  LikeCard,
  RateList,
  RateDetail,
  Team,
  AgentGoodDetails,
  Logistics,
  SubmitAgentOrder,
  Withdrawal,
  CvRanking,
  IosData,
  GoodsAppraise,
  PrivacyTerms,
} from './src/router/index.js'; //引入文件

import { TabBar } from './src/components/index.js'; //引入文件
// [RNSplashScreen show];
import { AppId, isUser, isWxUser, ScreenWidth, ScreenHeight, loadData, bodyPlus, Dimensions } from './src/util/utils.js'

import NetWork from './src/util/network.js'

// 修改defaultProps 设置allowFontScaling = false
TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, { defaultProps: false })
Text.defaultProps = Object.assign({}, Text.defaultProps, { allowFontScaling: false })
ScrollView.defaultProps = Object.assign({}, ScrollView.defaultProps, { showsVerticalScrollIndicator: false })



// const instructions = Platform.select({
//   ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
//   android:
//     'Double tap R on your keyboard to reload,\n' +
//     'Shake or press menu button for dev menu',
// });

const reducerCreate = params => {
  const defaultReducer = Reducer(params);
  return (state, action) => {
    return defaultReducer(state, action);
  }
};

export default class App extends Component {

  constructor(props) {
    super(props)
    this.state = {
      deviceList: []
    }
    WeChat.registerApp(AppId)
  }

  componentDidMount() {
    SplashScreen.hide()
    if (Platform.OS == 'ios') {
      StatusBarManager.getHeight(statusBarHeight => {
        store.dispatch({
          type: ActionTypes.setStatusHeight,
          data: statusBarHeight.height
        })
      })
    } else {
      store.dispatch({
        type: ActionTypes.setStatusHeight,
        data: StatusBar.currentHeight
      })
    }
    // loadData('loginInfo').then((loginInfo) => {
    //   if (loginInfo) {
    //     store.dispatch({
    //       type: ActionTypes.setLoginInfo,
    //       data: loginInfo,
    //     });
    //   }
    // })
    // loadData('userInfo').then((userInfo) => {
    //   if (userInfo) {
    //     store.dispatch({
    //       type: ActionTypes.setUserInfo,
    //       data: userInfo,
    //     });
    //     Actions.index();
    //   }
    // })
  }

  componentWillUnmount() {
    const { dispatch } = store
    dispatch({
      type: ActionTypes.clearMyDevice
    })
    bodyPlus.disConnect()
  }

  render() {
    const onBackPress = () => {
      if (Actions.state.index !== 0) {
        return false
      }
      Actions.pop()
      return true
    }
    const prefix = Platform.OS === 'android' ? 'mychat://mychat/' : 'mychat://';

    const stateHandler = (prevState, newState, action) => {
      // console.log('onStateChange: ACTION:', action);
    };

    const normalNav = { navigationBarStyle: { backgroundColor: '#EB4B6E', borderBottomWidth: 0, elevation: 0 }, titleStyle: { color: '#fff' } }
    return (
      <AntDesign.Provider>
        <Provider store={store}>
          {/* <SimpleApp /> */}
          {/* <Router backAndroidHandler={onBackPress} > */}
          <Router onStateChange={stateHandler} uriPrefix={prefix} createReducer={reducerCreate}>
            <Overlay key="overlay">
              <Modal key="modal" hideNavBar >
                {/* <Scene key="advertise" title="广告" component={Advertise} hideNavBar={true} {...normalNav} /> */}
                <Scene key="advertise" initial title="广告" component={Advertise} hideNavBar={true} {...normalNav} />
                {/* <Scene key="sssHome" initial title="广告" component={Home2} hideNavBar={true} {...normalNav} /> */}
                <Scene hideNavBar headerMode='screen' key="index" type={ActionConst.RESET}>
                  <Tabs
                    key="tabBar"        // 唯一标识
                    wrap={true}         // 自动使用自己的导航栏包装每个场景
                    showLabel={false}   // 显示文字
                    tabBarStyle={styles.tabbarContaine} // tabBar的样式
                    swipeEnabled={false}// 是否可以滑动
                    headerMode='screen' // 页面切换方式
                    icon={TabBar}      // 自定义Icon显示方式
                    lazy={true}         // 是否默认渲染tabbar
                    tabBarPosition={'bottom'}       // tabbar在顶部还是底部，iOS默认顶部，安卓默认顶部
                    tabBarOnPress={({ navigation, defaultHandler }) => {
                      let barStyle = navigation.state.routes[0].params.params
                      StatusBar.setBarStyle(barStyle && barStyle.type ? barStyle.type : 'dark-content', true);
                      Platform.OS == 'android' ? StatusBar.setBackgroundColor('rgba(0,0,0,0)', true) : '';//背景颜色是透明
                      Platform.OS == 'android' ? StatusBar.setTranslucent(true) : '';//设置状态栏透明
                      defaultHandler();//导航的默认行为，比如说标签导航下面的图标与颜色选中与未选中颜色的不同
                    }}
                  >
                    <Scene key="mall" navTransparent={true} title="商城" component={Mall} hideNavBar={true} sceneKey="mall"  {...normalNav} />
                    {/* <Scene key="health" navTransparent={true} title="健康" component={Health} hideNavBar={true} sceneKey="health"  {...normalNav} /> */}
                    <Scene key="guest" navTransparent={true} title="圈子" component={Guest} hideNavBar={true} sceneKey="guest"  {...normalNav} />
                    {Platform.OS != 'ios' && (<Scene key="data" navTransparent={true} title="数据" component={Data} hideNavBar={true} sceneKey="data"  {...normalNav} />)}
                    {Platform.OS == 'ios' && (<Scene key="data" navTransparent={true} title="数据" component={IosData} hideNavBar={true} sceneKey="data"  {...normalNav} />)}
                    <Scene key="user" params={{ type: 'light-content' }} renderTitle="light-content" navTransparent={true} title="我的" component={User} hideNavBar={true} sceneKey="user"  {...normalNav} />
                  </Tabs>
                </Scene>
                <Scene key="invite" navTransparent={true} title="邀请好友" component={Invite} hideNavBar={true} {...normalNav} />
                <Scene key="goodDetails" navTransparent={true} title="商品详情" component={GoodDetails} hideNavBar={true} {...normalNav} />
                <Scene key="shoppingCart" navTransparent={true} title="购物车" component={ShoppingCart} hideNavBar={true} {...normalNav} />
                <Scene key="submitOrder" navTransparent={true} title="提交订单" component={SubmitOrder} hideNavBar={true} {...normalNav} />
                <Scene key="paymentResult" navTransparent={true} title="支付结果" component={PaymentResult} hideNavBar={true} {...normalNav} />
                <Scene key="categories" navTransparent={true} title="商品分类" component={Categories} hideNavBar={true} {...normalNav} />
                <Scene key="order" navTransparent={true} title="订单列表" component={Order} hideNavBar={true} {...normalNav} />
                <Scene key="search" navTransparent={true} title="搜索" component={Search} hideNavBar={true} {...normalNav} />
                <Scene key="searchResults" navTransparent={true} title="搜索结果" component={SearchResults} hideNavBar={true} {...normalNav} />
                <Scene key="address" navTransparent={true} title="收货地址列表" component={Address} hideNavBar={true} {...normalNav} />
                <Scene key="creatAddress" navTransparent={true} title="新增收货地址" component={CreatAddress} hideNavBar={true} {...normalNav} />
                <Scene key="editAddress" navTransparent={true} title="编辑收货地址" component={EditAddress} hideNavBar={true} {...normalNav} />
                <Scene key="appraise" navTransparent={true} title="评论列表" component={Appraise} hideNavBar={true} {...normalNav} />
                <Scene key="orderInfo" navTransparent={true} title="订单详情" component={OrderInfo} hideNavBar={true} {...normalNav} />
                <Scene key="cvList" navTransparent={true} title="cv列表" component={CvList} hideNavBar={true} {...normalNav} />
                <Scene key="coinList" navTransparent={true} title="分身币列表" component={CoinList} hideNavBar={true} {...normalNav} />
                <Scene key="integral" navTransparent={true} title="积分列表" component={Integral} hideNavBar={true} {...normalNav} />
                <Scene key="integralRanking" navTransparent={true} title="积分排行" component={IntegralRanking} hideNavBar={true} {...normalNav} />
                <Scene key="coinRanking" navTransparent={true} title="分身币排行" component={CoinRanking} hideNavBar={true} {...normalNav} />
                <Scene key="agentGoods" navTransparent={true} title="分身币排行" component={AgentGoods} hideNavBar={true} {...normalNav} />
                <Scene key="agentProduct" navTransparent={true} title="分身币排行" component={AgentProduct} hideNavBar={true} {...normalNav} />
                <Scene key="userInfo" navTransparent={true} title="个人中心" component={UserInfo} hideNavBar={true} {...normalNav} />
                <Scene key="setting" navTransparent={true} title="设置" component={Setting} hideNavBar={true} {...normalNav} />
                <Scene key="changePassword" navTransparent={true} title="修改密码" component={ChangePassword} hideNavBar={true} {...normalNav} />
                <Scene key="selectPay" navTransparent={true} title="支付方式" component={SelectPay} hideNavBar={true} {...normalNav} />
                <Scene key="myRelatives" navTransparent={true} title="我的亲人" component={MyRelatives} hideNavBar={true} {...normalNav} />
                <Scene key="addMyRelatives" navTransparent={true} title="关联亲人" component={AddMyRelatives} hideNavBar={true} {...normalNav} />
                <Scene key="inviteFriends" navTransparent={true} title="邀请好友" component={InviteFriends} hideNavBar={true} {...normalNav} />
                <Scene key="data" navTransparent={true} title="数据" component={Data} hideNavBar={true} {...normalNav} />
                <Scene key="invoice" navTransparent={true} title="发票管理" component={Invoice} hideNavBar={true}  {...normalNav} />
                <Scene key="Editinvoice" navTransparent={true} title="编辑发票" component={EditInvoice} hideNavBar={true} {...normalNav} />
                <Scene key="creatInvoice" navTransparent={true} title="新增发票" component={CreatInvoice} hideNavBar={true} {...normalNav} />
                <Scene key="plateDetail" title="板块详情" component={PlateDetail} hideNavBar={true} {...normalNav} />
                <Scene key="cardPostDetail" title="帖子详情" component={CardPostDetail} hideNavBar={true} {...normalNav} />
                <Scene key="postCard" title="发布帖子" component={PostCard} hideNavBar={true} {...normalNav} />
                <Scene key="cardMyList" title="我发布的帖子" component={CardMyList} hideNavBar={true} {...normalNav} />
                <Scene key="cardUserInfo" title="帖子用户详情" component={CardUserInfo} hideNavBar={true} {...normalNav} />
                <Scene key="collectionCard" title="收藏的帖子" component={CollectionCard} hideNavBar={true} {...normalNav} />
                <Scene key="fansList" title="粉丝列表" component={FansList} hideNavBar={true} {...normalNav} />
                <Scene key="feedback" title="问题反馈" component={Feedback} hideNavBar={true} {...normalNav} />
                <Scene key="followAccount" title="关注的人" component={FollowAccount} hideNavBar={true} {...normalNav} />
                <Scene key="followPlate" title="关注的板块" component={FollowPlate} hideNavBar={true} {...normalNav} />
                <Scene key="likeCard" title="点赞的帖子" component={LikeCard} hideNavBar={true} {...normalNav} />
                <Scene key="rateList" title="心率列表" component={RateList} hideNavBar={true} {...normalNav} />
                <Scene key="rateDetail" title="心率详情" component={RateDetail} hideNavBar={true} {...normalNav} />
                <Scene key="setUserInfo" title="设置用户信息" component={SetUserInfo} hideNavBar={true} {...normalNav} />
                <Scene key="team" title="我的团队" component={Team} hideNavBar={true} {...normalNav} />
                <Scene key="agentGoodDetails" title="代理商品详情" component={AgentGoodDetails} hideNavBar={true} {...normalNav} />
                <Scene key="logistics" title="查看物流" component={Logistics} hideNavBar={true} {...normalNav} />
                <Scene key="submitAgentOrder" title="提交代理订单" component={SubmitAgentOrder} hideNavBar={true} {...normalNav} />
                <Scene key="phoneLogin" navTransparent={true} title="手机号登录" component={PhoneLogin} hideNavBar={true} {...normalNav} />
                <Scene key="login" navTransparent={true} title="登录" component={Login} hideNavBar={true} {...normalNav} />
                <Scene key="registered" navTransparent={true} title="注册" component={Registered} hideNavBar={true} {...normalNav} />
                <Scene key="withdrawal" navTransparent={true} title="提现" component={Withdrawal} hideNavBar={true} {...normalNav} />
                <Scene key="cvRanking" navTransparent={true} title="CV排行榜" component={CvRanking} hideNavBar={true} {...normalNav} />
                <Scene key="goodsAppraise" navTransparent={true} title="发布商品评论" component={GoodsAppraise} hideNavBar={true} {...normalNav} />
                <Scene key="privacyTerms" navTransparent={true} title="隐私条款" component={PrivacyTerms} hideNavBar={true} {...normalNav} />
              </Modal>
            </Overlay>
          </Router>
        </Provider>
      </AntDesign.Provider>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});
